<template>
  <div class="register-container">
    <a-card title="酒店管理系统注册" class="register-card">
      <a-form
        :model="formState"
        :rules="rules"
        @finish="handleRegister"
        layout="vertical"
      >
        <a-form-item label="用户名" name="username">
          <a-input v-model:value="formState.username" placeholder="请输入用户名" />
        </a-form-item>
        
        <a-form-item label="真实姓名" name="realName">
          <a-input v-model:value="formState.realName" placeholder="请输入真实姓名" />
        </a-form-item>
        
        <a-form-item label="密码" name="password">
          <a-input-password v-model:value="formState.password" placeholder="请输入密码" />
        </a-form-item>
        
        <a-form-item label="确认密码" name="confirmPassword">
          <a-input-password v-model:value="formState.confirmPassword" placeholder="请再次输入密码" />
        </a-form-item>
        
        <a-form-item label="电话" name="tel">
          <a-input v-model:value="formState.tel" placeholder="请输入电话号码" />
        </a-form-item>
        
        <a-form-item>
          <a-button type="primary" html-type="submit" :loading="loading" block>
            注册
          </a-button>
        </a-form-item>
        
        <div style="text-align: center;">
          <span>已有账号？</span>
          <a-button type="link" @click="goToLogin">立即登录</a-button>
        </div>
      </a-form>
    </a-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import axios from 'axios'

const router = useRouter()
const loading = ref(false)

const formState = reactive({
  username: '',
  realName: '',
  password: '',
  confirmPassword: '',
  tel: ''
})

const validateConfirmPassword = (_, value) => {
  if (value === '') {
    return Promise.reject(new Error('请确认密码'))
  } else if (value !== formState.password) {
    return Promise.reject(new Error('两次输入的密码不一致'))
  } else {
    return Promise.resolve()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6-20个字符之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  tel: [
    { required: true, message: '请输入电话号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
}

const handleRegister = async (values) => {
  loading.value = true
  try {
    // 创建用户对象
    const userData = {
      username: values.username,
      realName: values.realName,
      password: values.password, // 后端会进行BCrypt加密
      tel: values.tel
    }
    
    // 注意：这里需要调用用户创建接口，而不是专门的注册接口
    // 因为后端目前没有专门的注册接口，我们使用创建用户的接口
    const response = await axios.post('/api/users', userData)
    
    if (response.data.success) {
      message.success('注册成功！请登录')
      router.push('/login')
    } else {
      message.error(response.data.message || '注册失败')
    }
  } catch (error) {
    message.error(error.response?.data?.message || '注册失败')
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f0f2f5;
}

.register-card {
  width: 450px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style>